/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-01-12 21:44:35
 * @LastEditors: LIFM0623 1944099171@qq.com
 * @LastEditTime: 2024-03-29 15:43:36
 * @FilePath: \scene_back_nest\src\camera-rotate\camera-rotate.service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Injectable, NotFoundException } from '@nestjs/common';
import { createCameraRotateDto } from './dto/create-cameraRotate.dto';
import { updateCameraRotateDto } from './dto/update-cameraRotate.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { cameraRotate } from './entities/camera-rotate.entity';
import { Repository, In } from 'typeorm';

@Injectable()
export class CameraRotateService {

  constructor(
    @InjectRepository(cameraRotate)
    private readonly cameraRotateRepository: Repository<cameraRotate>,
  ) {}

  async remove(id: string) {
    const removeItem = await this.cameraRotateRepository.delete({ id: id });
    if (removeItem) {
      return '删除成功！';
    } else {
      return '删除失败！';
    }
  }

  async update(
    id: string,
    updateCameraItem: updateCameraRotateDto,
  ): Promise<cameraRotate | string> {
    const oldItem = await this.cameraRotateRepository.findOne({
      where: { id },
    });

    if (!oldItem) {
      throw new NotFoundException('该修改对象不存在');
    }

    const updatedCameraRotate = this.cameraRotateRepository.merge(
      oldItem,
      updateCameraItem,
    );

    await this.cameraRotateRepository.save(updatedCameraRotate);

    // 返回更新后的实体或其他有用的信息
    return updatedCameraRotate;
  }

  async findByIds(ids: string[]): Promise<cameraRotate[]> {
    return this.cameraRotateRepository.find({
      where: {
        id: In(ids), // 使用TypeORM的In操作符
      },
    });
  }

  async findOne(id: string): Promise<cameraRotate> {
    return this.cameraRotateRepository.findOne({ where: { id: id } });
  }
  async findUser(creator: string):Promise<cameraRotate[]> {
    return this.cameraRotateRepository.find({where:{creator:creator}})
  }
  async findAll() {
    return this.cameraRotateRepository.find();
  }
  async createCameraRotateItem(createCarmeraRotateDto: createCameraRotateDto) {
    const insertItem = this.cameraRotateRepository.create(
      createCarmeraRotateDto,
    );
    const insertRes = await this.cameraRotateRepository.insert(insertItem);
    return insertRes.raw;
  }

  async createCameraRotateItemForScript(createCarmeraRotateDto: createCameraRotateDto) {
    const insertItem = this.cameraRotateRepository.create(
      createCarmeraRotateDto,
    );
    const insertRes = await this.cameraRotateRepository.insert(insertItem);
    return insertRes.raw[0].id;
  }
}
